# 目标管理数据模型
from models import db
from datetime import datetime

class Goal(db.Model):
    """目标模型"""
    __tablename__ = 'goals'
    
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)  # 目标标题
    description = db.Column(db.Text)  # 目标描述
    category = db.Column(db.String(50), default='其他')  # 分类: 事业, 学习, 健康等
    target_date = db.Column(db.DateTime)  # 目标完成日期
    status = db.Column(db.String(20), default='进行中')  # 状态: 进行中, 已完成, 已放弃
    progress = db.Column(db.Integer, default=0)  # 进度百分比 (0-100)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)  # 创建时间
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)  # 更新时间
    
    # 关联子任务和里程碑
    tasks = db.relationship('GoalTask', backref='goal', lazy=True, cascade='all, delete-orphan')
    milestones = db.relationship('Milestone', backref='goal', lazy=True, cascade='all, delete-orphan')
    reflections = db.relationship('Reflection', backref='goal', lazy=True, cascade='all, delete-orphan')
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'title': self.title,
            'description': self.description,
            'category': self.category,
            'target_date': self.target_date.isoformat() if self.target_date else None,
            'status': self.status,
            'progress': self.progress,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat()
        }
    
    def __repr__(self):
        return f'<Goal {self.title}>'

class GoalTask(db.Model):
    """目标子任务模型"""
    __tablename__ = 'goal_tasks'
    
    id = db.Column(db.Integer, primary_key=True)
    goal_id = db.Column(db.Integer, db.ForeignKey('goals.id'), nullable=False)  # 关联目标
    title = db.Column(db.String(200), nullable=False)  # 任务标题
    completed = db.Column(db.Boolean, default=False)  # 完成状态
    order = db.Column(db.Integer, default=0)  # 排序
    created_at = db.Column(db.DateTime, default=datetime.utcnow)  # 创建时间
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'goal_id': self.goal_id,
            'title': self.title,
            'completed': self.completed,
            'order': self.order,
            'created_at': self.created_at.isoformat()
        }
    
    def __repr__(self):
        return f'<GoalTask {self.title}>'

class Milestone(db.Model):
    """里程碑模型"""
    __tablename__ = 'milestones'
    
    id = db.Column(db.Integer, primary_key=True)
    goal_id = db.Column(db.Integer, db.ForeignKey('goals.id'), nullable=False)  # 关联目标
    title = db.Column(db.String(200), nullable=False)  # 里程碑标题
    target_date = db.Column(db.DateTime)  # 目标日期
    completed = db.Column(db.Boolean, default=False)  # 完成状态
    completed_at = db.Column(db.DateTime)  # 完成时间
    created_at = db.Column(db.DateTime, default=datetime.utcnow)  # 创建时间
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'goal_id': self.goal_id,
            'title': self.title,
            'target_date': self.target_date.isoformat() if self.target_date else None,
            'completed': self.completed,
            'completed_at': self.completed_at.isoformat() if self.completed_at else None,
            'created_at': self.created_at.isoformat()
        }
    
    def __repr__(self):
        return f'<Milestone {self.title}>'

class Reflection(db.Model):
    """反思日记模型"""
    __tablename__ = 'reflections'
    
    id = db.Column(db.Integer, primary_key=True)
    goal_id = db.Column(db.Integer, db.ForeignKey('goals.id'), nullable=False)  # 关联目标
    content = db.Column(db.Text, nullable=False)  # 反思内容
    created_at = db.Column(db.DateTime, default=datetime.utcnow)  # 创建时间
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'goal_id': self.goal_id,
            'content': self.content,
            'created_at': self.created_at.isoformat()
        }
    
    def __repr__(self):
        return f'<Reflection for Goal {self.goal_id}>'

